/*请你仅使用两个栈实现先入先出队列。队列应当支持一般队列支持的所有操作（push、pop、peek、empty）：
* 实现 MyQueue 类：
* void push(int x) 将元素 x 推到队列的末尾
* int pop() 从队列的开头移除并返回元素
* int peek() 返回队列开头的元素
* boolean empty() 如果队列为空，返回 true ；否则，返回 false
* */

package newcoder.Stack_3;

import java.util.Arrays;
import java.util.Stack;

public class Leetcode232 {

    public static void main(String[] args) {

        MyQueue obj = new MyQueue();
        String[] str = {"MyQueue", "push", "push", "peek", "pop", "empty"};
        int[][] nums = {{}, {1}, {2}, {}, {}, {}};
        String[] res = new String[str.length];
        for (int i = 0;i < str.length;i++){

            if (str[i] == "MyQueue"){
                res[i] = null;
            }
            if (str[i] == "push"){
                obj.push(nums[i][0]);
                res[i] = null;
            }
            if (str[i] == "pop"){
                res[i] = Integer.toString(obj.pop());
            }
            if (str[i] == "peek"){
                res[i] = Integer.toString(obj.peek());
            }
            if (str[i] == "empty"){
                res[i] = Boolean.toString(obj.empty());
            }
        }
        System.out.println(Arrays.toString(res));
    }

}

class MyQueue{

    Stack<Integer> s1, s2;
    public MyQueue() {
        s1 = new Stack<>();
        s2 = new Stack<>();
    }

    public void push(int x){
        s1.push(x);
    }

    public int pop(){
        if (s2.isEmpty()){
            while(!s1.isEmpty()){
                s2.push(s1.pop());
            }
        }
        return s2.pop();
    }

    public int peek(){
        if (s2.isEmpty()){
            while (!s1.isEmpty()){
                s2.push(s1.pop());
            }
        }
        return s2.peek();
    }

    public boolean empty(){
        return s1.isEmpty() && s2.isEmpty();
    }
}
